import django_filters

from connection.models import Partner


class PartnerFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(method='get_name_filter')
    type = django_filters.CharFilter(method='get_type_filter')

    class Meta:
        model = Partner
        fields = ('name', 'type')

    def get_name_filter(self, queryset, name, value):
        if value:
            queryset = queryset.filter(name__icontains=value)
        return queryset

    def get_type_filter(self, queryset, name, value):
        if value:
            queryset = queryset.filter(type=value)
        return queryset
